package models.contract;

import models.UUIDModel;
import models.admin.AdminUserCampus;
import models.campus.Room;
import models.constants.DeletedStatus;
import org.hibernate.SQLQuery;
import org.hibernate.transform.Transformers;
import play.db.jpa.JPA;
import util.common.UStringUtil;

import javax.persistence.*;
import java.util.List;
import java.util.Map;

/**
 * 合同内 包含房间
 */
@Entity
@Table(name = "co_contract_rooms")
public class ContractRoom extends UUIDModel {

    /**
     * 合同信息
     */
    @JoinColumn(name = "contract_id")
    @ManyToOne
    public Contract contract;

    /**
     * 房间信息
     */
    @JoinColumn(name = "campus_room_id")
    @ManyToOne
    public Room room;

    /**
     * @param contractId
     * @return
     */
    public static List<ContractRoom> loadByContractId(String contractId) {
        return ContractRoom.find(" contract.id = ? and deleted = ? order by room.building.campus.id , room.building.name , room.roomNumber", contractId, DeletedStatus.UN_DELETED).fetch();
    }

    /**
     * 删除
     *
     * @param ids
     */
    public static void deletedContractRoom(String[] ids) {
        String idStr = "'" + UStringUtil.concatStr("','", ids) + "'";
        String sqlUpdate = " update co_contract_rooms a , bd_rooms b set a.deleted = 1 ,b.contract_status = 'NO_CONTRACT' where a.id in  (" + idStr + ") and a.campus_room_id = b.id";
        JPA.em().createNativeQuery(sqlUpdate).executeUpdate();
    }


    /**
     * 删除合同同时删除合同房间
     * @param contractId
     */
    public static void deleteContractRooms(String contractId) {
        String sqlUpdate = " update co_contract_rooms a , bd_rooms b set a.deleted = 1 ,b.contract_status = 'NO_CONTRACT' where a.contract_id = '"+contractId+"'  and a.campus_room_id = b.id";
        JPA.em().createNativeQuery(sqlUpdate).executeUpdate();
    }

    /**
     * 根据合同查询房间
     *
     * @param contract
     * @return
     */
    public static List<ContractRoom> findByContract(Contract contract) {
        return ContractRoom.find("contract = ? and deleted = ?", contract, DeletedStatus.UN_DELETED).fetch();
    }

    /**
     * 根据房间查询合同信息
     * @return
     */
    public static ContractRoom findByRoom(Room room) {
        return ContractRoom.find("room = ? and deleted = ?", room, DeletedStatus.UN_DELETED).first();
    }

    /**
     * 检查房间是否签订过合同
     * @return
     */
    public static  String checkIsContractRoom( String[] RoomIds){
        String idsStr = "'" + UStringUtil.concatStr("','",RoomIds)+"'" ;
        String roomStr = "";
        String sqlSelect=" SELECT b.room_number as roomNuber  from co_contract_rooms a left join bd_rooms b on a.campus_room_id=b.id where a.deleted = 0 and  b.id in(" + idsStr + ")group by b.id " ;
        Query query = Room.em().createNativeQuery(sqlSelect);
        query.unwrap(SQLQuery.class).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
        List<Map<String , Object>> resultList = query.getResultList();
        for(Map<String , Object> map : resultList){
            roomStr += map.get("roomNuber") + "," ;
        }
        return roomStr ;
    }


    /**
     * 根据用户查询和用户所管辖物业有关联的合同ID
     * @param adminUserid
     * @return
     */
    public static String getContractByAdminUser(long adminUserid) {
        String campusIds = AdminUserCampus.getCampusIdByAdminUser(adminUserid);
        String contractIds = "";
        if(campusIds.length() > 0) {
            List<ContractRoom> contractRoomList = ContractRoom.find(" deleted = ? and room.building.campus.id in (" + campusIds + ")", DeletedStatus.UN_DELETED).fetch();
            if (contractRoomList != null && contractRoomList.size() > 0) {
                for (ContractRoom contractRoom : contractRoomList) {
                    contractIds += ",'" + contractRoom.contract.getId() + "'";
                }
            }
        }

        return contractIds.length() > 0 ? contractIds.substring(1) : contractIds;

    }

}
